package com.aquarium.security;

import com.aquarium.util.SPUtils;

import android.content.Context;
import android.content.SharedPreferences;

public class SecurityManager {
	private static final String TAG = "SecurityManager";

	private static final String PREFER_NAME = "security";
	private static final String PASSWORD_KEY = "password";
	/**
	 * this is only used to reset controller password to factory setting.
	 */
	private static final String DEFAULT_PASSWORD = "0000";
	private static final String STORE_PASSWORD_KEY = "store_password";

	private static Context mContext;
	private SharedPreferences mSecurityPreferences;
	private static SecurityManager mSecurityManager;

	private SecurityManager() {
		mSecurityPreferences = mContext.getSharedPreferences(PREFER_NAME,
				Context.MODE_PRIVATE);
	}

	public static SecurityManager getInstance(Context context) {
		if (mSecurityManager == null) {
			mContext = context;
			mSecurityManager = new SecurityManager();
		}
		return mSecurityManager;
	}

	/**
	 * check whether user wanted to store the password.
	 * 
	 * @return true means user had chosen store password automatically.
	 */
	public boolean isStoredPassword() {

		return SPUtils.getSP(mSecurityPreferences, STORE_PASSWORD_KEY, false);
	}

	public void setNeedStorePassword(boolean needStorePassword) {
		SPUtils.setSP(mSecurityPreferences.edit(), STORE_PASSWORD_KEY,
				needStorePassword);
	}

	public void storePassword(String password) {
		SPUtils.setSP(mSecurityPreferences.edit(), PASSWORD_KEY, password);
	}

	public String getPassword() {

		return SPUtils.getSP(mSecurityPreferences, PASSWORD_KEY, "");
	}
}
